package com.newlink.building.common_base.utils

/**
 * 日志扩展方法 - 方便在现有代码中使用增强日志
 * @Author: Enhanced
 * @Date: 2025-09-15
 * @Description: 提供简洁的日志调用方式
 */

// ========== Fvl 对象的增强方法 ==========

/**
 * 扩展 Fvl 对象，添加增强日志功能
 */

// 网络请求日志
fun NLog.logNetwork(tag: String, url: String, method: String = "GET", params: String? = null, response: String? = null) {
    EnhancedLogger.network(tag, url, method, params, response)
}

// 网络错误日志
fun NLog.logNetworkError(tag: String, url: String, error: String, code: Int? = null) {
    EnhancedLogger.networkError(tag, url, error, code)
}

// JSON 日志
fun NLog.logJson(tag: String, title: String, jsonString: String?) {
    EnhancedLogger.json(tag, title, jsonString)
}

// 服务状态日志
fun NLog.logService(tag: String, serviceName: String, action: String, status: String) {
    EnhancedLogger.service(tag, serviceName, action, status)
}

// MQTT 消息日志
fun NLog.logMqtt(tag: String, topic: String, message: String, direction: String = "RECEIVE") {
    EnhancedLogger.mqtt(tag, topic, message, direction)
}

// 用户操作日志
fun NLog.logUserAction(tag: String, action: String, details: String? = null) {
    EnhancedLogger.userAction(tag, action, details)
}

// 生命周期日志
fun NLog.logLifecycle(tag: String, component: String, event: String, extra: String? = null) {
    EnhancedLogger.lifecycle(tag, component, event, extra)
}

// 性能日志
fun NLog.logPerformance(tag: String, operation: String, duration: Long, unit: String = "ms") {
    EnhancedLogger.performance(tag, operation, duration, unit)
}

// 方法追踪
fun NLog.logMethodEnter(tag: String, className: String, methodName: String, params: String? = null) {
    EnhancedLogger.methodTrace(tag, className, methodName, true, params)
}

fun NLog.logMethodExit(tag: String, className: String, methodName: String) {
    EnhancedLogger.methodTrace(tag, className, methodName, false)
}

// 分隔线
fun NLog.logSeparator(tag: String, title: String = "") {
    EnhancedLogger.separator(tag, title)
}

// 成功日志
fun NLog
    .logSuccess(tag: String, message: String) {
    EnhancedLogger.success(tag, message)
}

// ========== 全局扩展函数 ==========

/**
 * Activity/Fragment 生命周期日志
 */
inline fun <reified T> T.logLifecycle(event: String, extra: String? = null) where T : Any {
    val className = T::class.java.simpleName
    EnhancedLogger.lifecycle(className, className, event, extra)
}

/**
 * 网络请求的简化日志
 */
fun logNetworkRequest(tag: String, url: String, method: String = "GET") {
    EnhancedLogger.network(tag, url, method)
}

fun logNetworkResponse(tag: String, url: String, response: String) {
    EnhancedLogger.network(tag, url, "RESPONSE", response = response)
}

fun logNetworkError(tag: String, url: String, error: Throwable) {
    EnhancedLogger.networkError(tag, url, error.message ?: "Unknown error")
}

/**
 * MQTT 消息的简化日志
 */
fun logMqttSend(tag: String, topic: String, message: String) {
    EnhancedLogger.mqtt(tag, topic, message, "SEND")
}

fun logMqttReceive(tag: String, topic: String, message: String) {
    EnhancedLogger.mqtt(tag, topic, message, "RECEIVE")
}

/**
 * 服务状态的简化日志
 */
fun logServiceStart(tag: String, serviceName: String) {
    EnhancedLogger.service(tag, serviceName, "START", "STARTING")
}

fun logServiceStop(tag: String, serviceName: String) {
    EnhancedLogger.service(tag, serviceName, "STOP", "STOPPING")
}

fun logServiceConnected(tag: String, serviceName: String) {
    EnhancedLogger.service(tag, serviceName, "CONNECT", "CONNECTED")
}

fun logServiceDisconnected(tag: String, serviceName: String) {
    EnhancedLogger.service(tag, serviceName, "DISCONNECT", "DISCONNECTED")
}

/**
 * 性能监控扩展
 */
inline fun <T> measurePerformance(tag: String, operation: String, block: () -> T): T {
    val startTime = System.currentTimeMillis()
    return try {
        block()
    } finally {
        val duration = System.currentTimeMillis() - startTime
        EnhancedLogger.performance(tag, operation, duration)
    }
}

/**
 * 带位置信息的日志
 */
inline fun <reified T> logDebug(message: String, throwable: Throwable? = null) {
    EnhancedLogger.logWithLocation<T>(EnhancedLogger.LogLevel.DEBUG, message, throwable)
}

inline fun <reified T> logError(message: String, throwable: Throwable? = null) {
    EnhancedLogger.logWithLocation<T>(EnhancedLogger.LogLevel.ERROR, message, throwable)
}

inline fun <reified T> logInfo(message: String, throwable: Throwable? = null) {
    EnhancedLogger.logWithLocation<T>(EnhancedLogger.LogLevel.INFO, message, throwable)
}

inline fun <reified T> logWarn(message: String, throwable: Throwable? = null) {
    EnhancedLogger.logWithLocation<T>(EnhancedLogger.LogLevel.WARN, message, throwable)
}